var FacilityNew = FacilityNew || {};

FacilityNew = function () {
  this.baseSize=40;
  this.cursorMesh=null;
  this.facMesh=null;
  this.isBind=false;
  this.goodsSession={rndId:null,gid:null,
             position:{x:0,y:0,z:0},
             rotation:{x:0,y:0,z:0}};
  this.mouse2D= new THREE.Vector3( 0, 0, 0);
  this.centerOffset=new THREE.Vector3(0,0,0);
  this.scene=null;
  
  this.keyFront=false;
    this.keyBack=false;
  this.keyRight=false;
  this.keyLeft=false;
  this.keyUp=false;
  this.keyDown=false;
  this.keySpace=false;
  
  this.release=function(houseSession){
    this.bind=false;
    this.goodsSession.position.x=this.facMesh.position.x;
    this.goodsSession.position.y=this.facMesh.position.y;
    this.goodsSession.position.z=this.facMesh.position.z;
    this.goodsSession.rotation.x=this.facMesh.rotation.x;
    this.goodsSession.rotation.y=this.facMesh.rotation.y;
    this.goodsSession.rotation.z=this.facMesh.rotation.z;

    this.cursorMesh.visible=true;
    if(houseSession){
      me.houseSession.push({gid:this.goodsSession.gid,
                position:{x:this.facMesh.position.x,y:this.facMesh.position.y,z:this.facMesh.position.z},
                rotation:{x:this.facMesh.rotation.x,y:this.facMesh.rotation.y,z:this.facMesh.rotation.z}
      });
    }
    this.facMesh=null;
  };
  
  // this.control=function() {
        // var speed = 10;
        // var halfSize = 50;
        // var nearHalfSize = halfSize-5;
    // var gravity = 1;
        // if (this.keyFront) {
          // this.facMesh.position.z -= speed;
        // }
        // if (this.keyBack) {
          // this.facMesh.position.z += speed;
        // }
        // if (this.keyLeft) {
          // this.facMesh.position.x -= speed;
        // }
        // if (this.keyRight) {
          // this.facMesh.position.x += speed;
        // }
        // // if(this.keyUp){
          // // this.facMesh.position.y+=speed;
        // // }
        // // if(this.keyDown){
          // // this.facMesh.position.y-=speed;
        // // }
        // // if (this.keySpace && onGround) {
          // // yspeed = 30;
          // // onGround = false;
        // // }
// //       
        // // // collideMesh.position.y = 6000;
// //         
        // // onGround = false;
        // // // y
        // // yspeed -= gravity;
        // // this.facMesh.position.y += yspeed;
//         
        // // var down_vector = new THREE.Vector3( 0, -1, 0 );
        // // var up_vector = new THREE.Vector3( 0, 1, 0 );
// // // 
        // // var bl = new THREE.Vector3( this.facMesh.position.x-halfSize, this.facMesh.position.y, this.facMesh.position.z-halfSize );
        // // var br = new THREE.Vector3( this.facMesh.position.x+halfSize, this.facMesh.position.y, this.facMesh.position.z-halfSize );
        // // var fl = new THREE.Vector3( this.facMesh.position.x-halfSize, this.facMesh.position.y, this.facMesh.position.z+halfSize );
        // // var fr = new THREE.Vector3( this.facMesh.position.x+halfSize, this.facMesh.position.y, this.facMesh.position.z+halfSize );
// // //         
        // // // // down
        // // if (this.keyDown) {
          // // // bl
          // // var bl_ray = new THREE.Ray( bl, down_vector );
          // // var bl_intersects = bl_ray.intersectScene( this.scene );
          // // if ( bl_intersects.length > 0 && bl_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = bl_intersects[0].point.y+halfSize;
            // // // onGround = true;
          // // }
          // // // br
          // // var br_ray = new THREE.Ray( br, down_vector );
          // // var br_intersects = br_ray.intersectScene( this.scene );
          // // if ( br_intersects.length > 0 && br_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = br_intersects[0].point.y+halfSize;
            // // // onGround = true;
          // // }
          // // // fl
          // // var fl_ray = new THREE.Ray( fl, down_vector );
          // // var fl_intersects = fl_ray.intersectScene( this.scene );
          // // if ( fl_intersects.length > 0 && fl_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = fl_intersects[0].point.y+halfSize;
            // // // onGround = true;
          // // } 
          // // // fr
          // // var fr_ray = new THREE.Ray( fr, down_vector );
          // // var fr_intersects = fr_ray.intersectScene( this.scene );
          // // if ( fr_intersects.length > 0 && fr_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = fr_intersects[0].point.y+halfSize;
            // // // onGround = true;
          // // } 
        // // }
        // // // up
        // // if (this.keyUp) {
          // // // bl
          // // var bl_ray = new THREE.Ray( bl, up_vector );
          // // var bl_intersects = bl_ray.intersectScene( this.scene );
          // // if ( bl_intersects.length > 0 && bl_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = bl_intersects[0].point.y-halfSize;
          // // }
          // // // br
          // // var br_ray = new THREE.Ray( br, up_vector );
          // // var br_intersects = br_ray.intersectScene( this.scene );
          // // if ( br_intersects.length > 0 && br_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = br_intersects[0].point.y-halfSize;
          // // }
          // // // fl
          // // var fl_ray = new THREE.Ray( fl, up_vector );
          // // var fl_intersects = fl_ray.intersectScene( this.scene );
          // // if ( fl_intersects.length > 0 && fl_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = fl_intersects[0].point.y-halfSize;
          // // } 
          // // // fr
          // // var fr_ray = new THREE.Ray( fr, up_vector );
          // // var fr_intersects = fr_ray.intersectScene( this.scene );
          // // if ( fr_intersects.length > 0 && fr_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = fr_intersects[0].point.y-halfSize;
          // // } 
        // // }
// //         
        // // if (onGround) {
          // // yspeed = 0;
        // // }
// 
        // // this.facMesh.updateMatrix();
// 
        // // ray
        // var f_vector = new THREE.Vector3( 0, 0, -1 );
        // var b_vector = new THREE.Vector3( 0, 0, 1 );
        // var l_vector = new THREE.Vector3( -1, 0, 0 );
        // var r_vector = new THREE.Vector3( 1, 0, 0 );
        // var u_vector = new THREE.Vector3( 0, 1, 0 );
        // var d_vector = new THREE.Vector3( 0, -1, 0 );
// 
        // var left = new THREE.Vector3( this.facMesh.position.x-halfSize, this.facMesh.position.y-nearHalfSize, this.facMesh.position.z );
        // var right = new THREE.Vector3( this.facMesh.position.x+halfSize, this.facMesh.position.y-nearHalfSize, this.facMesh.position.z );
        // var front = new THREE.Vector3( this.facMesh.position.x, this.facMesh.position.y-nearHalfSize, this.facMesh.position.z+halfSize );
        // var back = new THREE.Vector3( this.facMesh.position.x, this.facMesh.position.y-nearHalfSize, this.facMesh.position.z-halfSize );
    // var up = new THREE.Vector3( this.facMesh.position.x-nearHalfSize, this.facMesh.position.y, this.facMesh.position.z-halfSize );
        // var down = new THREE.Vector3( this.facMesh.position.x-nearHalfSize, this.facMesh.position.y, this.facMesh.position.z+halfSize);
        // //up
        // // if(this.keyUp){
          // // var left_ray = new THREE.Ray( left, f_vector );
          // // var left_intersects = left_ray.intersectScene( this.scene );
          // // if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = left_intersects[0].point.y+halfSize+1;
          // // }
          // // var right_ray = new THREE.Ray( right, f_vector );
          // // var right_intersects = right_ray.intersectScene( this.scene  );
          // // if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = right_intersects[0].point.y+halfSize+1;
          // // }
// //           
        // // }
    // // if(this.keyDown){
          // // var left_ray = new THREE.Ray( left, f_vector );
          // // var left_intersects = left_ray.intersectScene( this.scene );
          // // if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = left_intersects[0].point.y-halfSize-1;
          // // }
          // // var right_ray = new THREE.Ray( right, f_vector );
          // // var right_intersects = right_ray.intersectScene( this.scene  );
          // // if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
            // // this.facMesh.position.y = right_intersects[0].point.y-halfSize-1;
          // // }
// //           
        // // }
        // // front
        // if (this.keyFront) {
          // var left_ray = new THREE.Ray( left, f_vector );
          // var left_intersects = left_ray.intersectScene( this.scene );
          // if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
            // this.facMesh.position.z = left_intersects[0].point.z+halfSize+1;
          // }
          // var right_ray = new THREE.Ray( right, f_vector );
          // var right_intersects = right_ray.intersectScene( this.scene  );
          // if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
            // this.facMesh.position.z = right_intersects[0].point.z+halfSize+1;
          // }
        // }
        // // back
        // if (this.keyBack) {
          // var left_ray = new THREE.Ray( left, b_vector );
          // var left_intersects = left_ray.intersectScene( this.scene  );
          // if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
            // this.facMesh.position.z = left_intersects[0].point.z-halfSize-1;
          // }
          // var right_ray = new THREE.Ray( right, b_vector );
          // var right_intersects = right_ray.intersectScene( this.scene  );
          // if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
            // this.facMesh.position.z = right_intersects[0].point.z-halfSize-1;
          // }
        // }       
        // // right
        // if (this.keyRight) {
          // var back_ray = new THREE.Ray( back, r_vector );
          // var back_intersects = back_ray.intersectScene( this.scene  );
          // if ( back_intersects.length > 0 && back_intersects[0].distance < halfSize ) {
            // this.facMesh.position.x = back_intersects[0].point.x-halfSize-1;
          // }
          // var front_ray = new THREE.Ray( front, r_vector );
          // var front_intersects = front_ray.intersectScene( this.scene  );
          // if ( front_intersects.length > 0 && front_intersects[0].distance < halfSize ) {
            // this.facMesh.position.x = front_intersects[0].point.x-halfSize-1;
          // }
        // }
        // // left
        // if (this.keyLeft) {
          // var back_ray = new THREE.Ray( back, l_vector );
          // var back_intersects = back_ray.intersectScene( this.scene  );
          // if ( back_intersects.length > 0 && back_intersects[0].distance < halfSize ) {
            // this.facMesh.position.x = back_intersects[0].point.x+halfSize+1;
          // }
          // var front_ray = new THREE.Ray( front, l_vector );
          // var front_intersects = front_ray.intersectScene( this.scene  );
          // if ( front_intersects.length > 0 && front_intersects[0].distance < halfSize ) {
            // this.facMesh.position.x = front_intersects[0].point.x+halfSize+1;
          // }
        // }
        // // //up
        // // if(this.keyUp){
          // // var
        // // }
        // console.warn('mesh position');
        // console.warn(this.facMesh.position);
        // this.facMesh.updateMatrix();
        // // collideMesh.position.y += 100;
     // };
// };
this.control2=function() {
        var speed = 10;
        var halfSize = this.baseSize;
        var nearHalfSize = halfSize-5;
    var gravity = 1;

        // ray
        var f_vector = new THREE.Vector3( 0, 0, -1 );
        var b_vector = new THREE.Vector3( 0, 0, 1 );
        var l_vector = new THREE.Vector3( -1, 0, 0 );
        var r_vector = new THREE.Vector3( 1, 0, 0 );
        var u_vector = new THREE.Vector3( 0, 1, 0 );
        var d_vector = new THREE.Vector3( 0, -1, 0 );

        var left = new THREE.Vector3( this.facMesh.position.x-halfSize, this.facMesh.position.y-nearHalfSize, this.facMesh.position.z );
        var right = new THREE.Vector3( this.facMesh.position.x+halfSize, this.facMesh.position.y-nearHalfSize, this.facMesh.position.z );
        var front = new THREE.Vector3( this.facMesh.position.x, this.facMesh.position.y-nearHalfSize, this.facMesh.position.z+halfSize );
        var back = new THREE.Vector3( this.facMesh.position.x, this.facMesh.position.y-nearHalfSize, this.facMesh.position.z-halfSize );
    var up = new THREE.Vector3( this.facMesh.position.x-nearHalfSize, this.facMesh.position.y, this.facMesh.position.z-halfSize );
        var down = new THREE.Vector3( this.facMesh.position.x-nearHalfSize, this.facMesh.position.y, this.facMesh.position.z+halfSize);
        
        // if(this.keyFront){
        // front
          var left_ray = new THREE.Ray( left, f_vector );
          var left_intersects = left_ray.intersectScene( this.scene );
          if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
            this.facMesh.position.z = left_intersects[0].point.z+halfSize+1;
          }
          var right_ray = new THREE.Ray( right, f_vector );
          var right_intersects = right_ray.intersectScene( this.scene  );
          if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
            this.facMesh.position.z = right_intersects[0].point.z+halfSize+1;
          }
         // }
         
         // if(this.keyBack){
        // back

          var left_ray = new THREE.Ray( left, b_vector );
          var left_intersects = left_ray.intersectScene( this.scene  );
          if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
            this.facMesh.position.z = left_intersects[0].point.z-halfSize-1;
          }
          var right_ray = new THREE.Ray( right, b_vector );
          var right_intersects = right_ray.intersectScene( this.scene  );
          if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
            this.facMesh.position.z = right_intersects[0].point.z-halfSize-1;
          }  
          // }   
          // if(this.keyRight){
        // right
          var back_ray = new THREE.Ray( back, r_vector );
          var back_intersects = back_ray.intersectScene( this.scene  );
          if ( back_intersects.length > 0 && back_intersects[0].distance < halfSize ) {
            this.facMesh.position.x = back_intersects[0].point.x-halfSize-1;
          }
          var front_ray = new THREE.Ray( front, r_vector );
          var front_intersects = front_ray.intersectScene( this.scene  );
          if ( front_intersects.length > 0 && front_intersects[0].distance < halfSize ) {
            this.facMesh.position.x = front_intersects[0].point.x-halfSize-1;
          }
          // }
          // if(this.keyLeft){         
          // left
            var back_ray = new THREE.Ray( back, l_vector );
            var back_intersects = back_ray.intersectScene( this.scene  );
            if ( back_intersects.length > 0 && back_intersects[0].distance < halfSize ) {
              this.facMesh.position.x = back_intersects[0].point.x+halfSize+1;
            }
            var front_ray = new THREE.Ray( front, l_vector );
            var front_intersects = front_ray.intersectScene( this.scene  );
            if ( front_intersects.length > 0 && front_intersects[0].distance < halfSize ) {
              this.facMesh.position.x = front_intersects[0].point.x+halfSize+1;
            }
           // }
              
          var down_vector = new THREE.Vector3( 0, -1, 0 );
          var up_vector = new THREE.Vector3( 0, 1, 0 );
  
          var bl = new THREE.Vector3( this.facMesh.position.x-halfSize, this.facMesh.position.y, this.facMesh.position.z-halfSize );
          var br = new THREE.Vector3( this.facMesh.position.x+halfSize, this.facMesh.position.y, this.facMesh.position.z-halfSize );
          var fl = new THREE.Vector3( this.facMesh.position.x-halfSize, this.facMesh.position.y, this.facMesh.position.z+halfSize );
          var fr = new THREE.Vector3( this.facMesh.position.x+halfSize, this.facMesh.position.y, this.facMesh.position.z+halfSize );
    
          //down
              var bl_down_ray = new THREE.Ray( bl, down_vector );
          var bl_down_intersects = bl_down_ray.intersectScene( this.scene );
          if ( bl_down_intersects.length > 0 && bl_down_intersects[0].distance < halfSize ) {
            this.facMesh.position.y = bl_down_intersects[0].point.y+halfSize;
          }
          // br
          var br_down_ray = new THREE.Ray( br, down_vector );
          var br_down_intersects = br_down_ray.intersectScene( this.scene );
          if ( br_down_intersects.length > 0 && br_down_intersects[0].distance < halfSize ) {
            this.facMesh.position.y = br_down_intersects[0].point.y+halfSize;
          }
          // fl
          var fl_down_ray = new THREE.Ray( fl, down_vector );
          var fl_down_intersects = fl_down_ray.intersectScene( this.scene );
          if ( fl_down_intersects.length > 0 && fl_down_intersects[0].distance < halfSize ) {
            this.facMesh.position.y = fl_down_intersects[0].point.y+halfSize;
          } 
          // fr
          var fr_down_ray = new THREE.Ray( fr, down_vector );
          var fr_down_intersects = fr_down_ray.intersectScene( this.scene );
          if ( fr_down_intersects.length > 0 && fr_down_intersects[0].distance < halfSize ) {
            this.facMesh.position.y = fr_down_intersects[0].point.y+halfSize;
          }

          //up
          var bl_up_ray = new THREE.Ray( bl, up_vector );
          var bl_up_intersects = bl_up_ray.intersectScene( this.scene );
          if ( bl_up_intersects.length > 0 && bl_up_intersects[0].distance < halfSize ) {     
            this.facMesh.position.y = bl_up_intersects[0].point.y-halfSize;
          }
          // br
          var br_up_ray = new THREE.Ray( br, up_vector );
          var br_up_intersects = br_up_ray.intersectScene( this.scene );
          if ( br_up_intersects.length > 0 && br_up_intersects[0].distance < halfSize ) {  
            this.facMesh.position.y = br_up_intersects[0].point.y-halfSize;
          }
          // fl
          var fl_up_ray = new THREE.Ray( fl, up_vector );
          var fl_up_intersects = fl_up_ray.intersectScene( this.scene );
          if ( fl_up_intersects.length > 0 && fl_up_intersects[0].distance < halfSize ) { 
            this.facMesh.position.y = fl_up_intersects[0].point.y-halfSize;
          } 
          // fr
          var fr_up_ray = new THREE.Ray( fr, up_vector );
          var fr_up_intersects = fr_up_ray.intersectScene( this.scene );
          if ( fr_up_intersects.length > 0 && fr_up_intersects[0].distance < halfSize ) {
            this.facMesh.position.y = fr_up_intersects[0].point.y-halfSize;
          }   
          
        this.facMesh.updateMatrix();
        // collideMesh.position.y += 100;
     };
};
FacilityNew.prototype = {

  constructor:FacilityNew,
  init:function(scene){
    this.scene=scene;
    cursorGeo = new THREE.CubeGeometry( this.baseSize, 10, this.baseSize );
    cursorGeo.computeBoundingBox();
    this.centerOffset.copy(cursorGeo.boundingBox.max.subSelf(cursorGeo.boundingBox.min));
        cursorMaterial = new THREE.MeshBasicMaterial( { color: 0x654321, opacity: 0.5, transparent: true } );
        this.cursorMesh = new THREE.Mesh( cursorGeo, cursorMaterial );
        this.cursorMesh.name="cursor";
    scene.add(this.cursorMesh);
  },
  load:function(goodsGeo,scene,goodsTyp,houseSession){
    var me=this;
        var loader = new THREE.JSONLoader();
    // scene.remove(this.cursorMesh);
    if(!goodsTyp) this.cursorMesh.visible=false;
    loader.load( goodsGeo.geometry, function( geometry ) {
      me.goodsSession.gid=goodsGeo._id;
        geometry.computeBoundingBox();
      // me.centerOffset.x =  Math.round(geometry.boundingBox.x[ 1 ] - geometry.boundingBox.x[ 0 ]) ;
      // me.centerOffset.y =  Math.round(geometry.boundingBox.y[ 1 ] - geometry.boundingBox.y[ 0 ]) ;
      // me.centerOffset.z =  Math.round(geometry.boundingBox.z[ 1 ] - geometry.boundingBox.z[ 0 ]) ;
      
      me.centerOffset.copy(geometry.boundingBox.max.subSelf(geometry.boundingBox.min));
      // console.log(me.centerOffset.x+","+me.centerOffset.y+","+me.centerOffset.z+";");
      rollOverMaterial = new THREE.MeshBasicMaterial( { color: 0xFF99FF, opacity: 0.5, transparent: true } );
          matrials=new Array();
      matrials.push(new THREE.MeshFaceMaterial());
      matrials.push(rollOverMaterial);
      
      var position=new THREE.Vector3;
      if(goodsTyp)
           position.set(parseInt(goodsTyp.position.x),parseInt(goodsTyp.position.y),parseInt(goodsTyp.position.z));
      else 
           position.copy(me.cursorMesh.position);
           
      rollOverGeo = new THREE.CubeGeometry( me.baseSize*2, me.baseSize*2, me.baseSize*2 );
      rollOverGeo.computeBoundingBox();
      me.centerOffset.copy(rollOverGeo.boundingBox.max.subSelf(rollOverGeo.boundingBox.min));
      rollOverMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.5, transparent: true } );     
      me.facMesh = new THREE.Mesh( rollOverGeo, rollOverMaterial );
      // me.facMesh = new THREE.Mesh( geometry, matrials );
      me.facMesh.position.copy(position);
      me.facMesh.name='fac';
      
      me.facMesh._id=goodsGeo._id;
      today=new Date();
      rndId=Math.ceil((today.getTime()*9301+49297)%233280/233280.0*511555);
      me.facMesh.rndId=rndId;
      me.goodsSession.rndId=rndId;
      scene.add(me.facMesh); 
      me.facMesh.overdraw = true;
      me.facMesh.doubleSided = false;
      if(goodsTyp)me.release(houseSession);
    });
    
  },
  move:function(mouse2D){
    this.mouse2D=mouse2D;
  },
  setOnBoard:function(plane,scene,camera){

    projector = new THREE.Projector();
    ray = projector.pickingRay(this.mouse2D.clone(),camera );
        var intersects = ray.intersectScene( scene );
      if ( intersects.length > 0 ) {
            intersector = this.getRealIntersector( intersects,plane );
            if ( intersector ) {
               
                this.setPosition( intersector );
                //if(intersector.object.name='wall') this.cursorMesh.rotation.x = - 90 * Math.PI / 180;
             }
        }
  },
  getOnBoard:function(plane,scene,camera){
    // mouse3D = this.projector.unprojectVector( mouse2D.clone(), this.camera );
    projector = new THREE.Projector();
    // ray = projector.pickingRay(this.mouse2D.clone(),camera );
        var intersects = ray.intersectScene( scene );
      if ( intersects.length > 0 ) {
      // // intersector = this.getRealIntersector( intersects,plane );
      tor=null;
      // for( i = 0; i < intersects.length; i++ ) {
      //       intersector = intersects[ i ];
      //        if ( intersector.object.name !="background" && 
      //           intersector.object.name!='wall' && 
      //           intersector.object.name!='floor'&&
      //           intersector.object.name !='cursor') {
      //                tor= intersector;
      //  }
      //            }
          tor=intersects[0];
      if ( tor ) {
        this.facMesh=tor.object;
        this.cursorMesh.visible=false;
        this.setPosition( intersector );        
      }
        }
  },
  setPosition:function (intersector ) {
    var tmpVec = new THREE.Vector3();
    var voxelPosition = new THREE.Vector3();
        tmpVec.copy( intersector.face.normal );
        voxelPosition.add( intersector.point, intersector.object.matrixRotationWorld.multiplyVector3( tmpVec ) );
        // console.warn(voxelPosition);
        
    // the codes below should be checked and tested.
            // voxelPosition.x = Math.floor( voxelPosition.x / this.centerOffset.z ) * this.centerOffset.z + this.centerOffset.z/2;
            // voxelPosition.y = Math.floor( voxelPosition.y / this.centerOffset.y ) * this.centerOffset.y + this.centerOffset.y/2;
            // voxelPosition.z = Math.floor( voxelPosition.z / this.centerOffset.x ) * this.centerOffset.x + this.centerOffset.x/2;
    
    
        voxelPosition.x = Math.floor( voxelPosition.x / this.baseSize ) * this.baseSize + 20;
        voxelPosition.y = Math.floor( voxelPosition.y / 10 ) * 10 + 5;
        voxelPosition.z = Math.floor( voxelPosition.z / this.baseSize ) * this.baseSize + 20;

      if(this.facMesh){
            if(voxelPosition.x<this.facMesh.position.x){
              this.keyLeft=true;
              this.keyRight=false;
            }else if(voxelPosition.x>this.facMesh.position.x){
              this.keyRight=true;
              this.keyLeft=false;
            }else {
              this.keyRight=false;
        this.keyLeft=false;
            }
            if(voxelPosition.z<this.facMesh.position.z){
              this.keyFront=true;
              this.keyBack=false;
            }else if(voxelPosition.z>this.facMesh.position.z){
              this.keyFront=false;
              this.keyBack=true;
            }else {
              this.keyFront=false;
              this.keyBack=false;
            }
      if(voxelPosition.y<this.facMesh.position.y){
              this.keyUp=true;
              this.keyDown=false;
            }else if(voxelPosition.y>this.facMesh.position.y){
              this.keyUp=false;
              this.keyDown=true;
              
            }else {
              this.keyUp=false;
              this.keyDown=false;
            }
            // this.control();
            
            
            this.facMesh.position.copy(voxelPosition);
            this.control2();
            // // this.facMesh.position.y=40;
            // this.facMesh.updateMatrix();
          }
          
         //未作碰撞检测
                    
          // this.cursorMesh.position.copy(voxelPosition);
          // this.cursorMesh.updateMatrix();
     },
        control:this.control2,
  getRealIntersector:function(intersects,flat){
    var isInObj=false;
    for( i = 0; i < intersects.length; i++ ) {
            intersector = intersects[ i ];
      if ( intersector.object !=this.cursorMesh && intersector.object !=this.facMesh) {
              return intersector;
      }
    }
        return null;
  },
     
  bind:function(){
    this.bind=true;
  },
  release:this.release,
  rotate:function(){
    
  },
  clear:function(){
    this.bind=false;
    scene.remove(this.facMesh);
    this.facMesh=null;
  },
  rand:function(number){

  }
  
}